package com.kstech.gw.fragment;

import android.app.Fragment;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.CoordinateConverter;
import com.amap.api.location.DPoint;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.GroundOverlayOptions;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;
import com.amap.api.maps2d.model.MyLocationStyle;
import com.amap.api.maps2d.model.PolylineOptions;
import com.amap.api.services.core.LatLonPoint;
import com.kstech.gw.R;
import com.kstech.gw.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lenovo on 2017/1/11.
 */

public class MapFragment extends Fragment implements View.OnClickListener{
    private MapView mapView;
    private AMap aMap;
    private Button basicmap;
    private Button rsmap;
    private Button showposition;
    private TextView mLocationErrText;
    private static final int STROKE_COLOR = Color.argb(180, 3, 145, 255);
    private static final int FILL_COLOR = Color.argb(10, 0, 0, 180);

    private RadioGroup mRadioGroup;

    /**
     * 缩放级别  zoom  3.0 ----  19.0
     * @param savedInstanceState
     */


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = View.inflate(getActivity(), R.layout.fragment_map,null);
        mapView = (MapView) view.findViewById(R.id.fragment_map);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        init(view);
        return view;
    }

    /**
     * 初始化AMap对象
     */
    private void init(View view) {
        if (aMap == null) {
            aMap = mapView.getMap();
            setUpMap();
        }
        basicmap = (Button)view.findViewById(R.id.basicmap);
        basicmap.setOnClickListener(this);
        rsmap = (Button)view.findViewById(R.id.rsmap);
        rsmap.setOnClickListener(this);
        showposition = (Button)view.findViewById(R.id.btn_show);
        showposition.setOnClickListener(this);

        mRadioGroup = (RadioGroup) view.findViewById(R.id.check_language);
        mLocationErrText = (TextView)view.findViewById(R.id.location_errInfo_text);
        mLocationErrText.setVisibility(View.GONE);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radio_en) {
                    aMap.setMapLanguage(AMap.ENGLISH);
                } else {
                    aMap.setMapLanguage(AMap.CHINESE);
                }
            }
        });
        aMap.moveCamera(CameraUpdateFactory.zoomTo(12));
    }

    private void setUpMap() {
        //aMap.setLocationSource(this);// 设置定位监听
        //aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
        //aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位，false表示隐藏定位层并不可触发定位，默认是false
        setupLocationStyle();
    }

    private void setupLocationStyle(){
        // 自定义系统定位蓝点
        MyLocationStyle myLocationStyle = new MyLocationStyle();
        // 自定义定位蓝点图标
        myLocationStyle.myLocationIcon(BitmapDescriptorFactory.
                fromResource(R.drawable.gps_point));
        // 自定义精度范围的圆形边框颜色
        myLocationStyle.strokeColor(STROKE_COLOR);
        //自定义精度范围的圆形边框宽度
        myLocationStyle.strokeWidth(5);
        // 设置圆形的填充颜色
        myLocationStyle.radiusFillColor(FILL_COLOR);
        // 将自定义的 myLocationStyle 对象添加到地图上
        aMap.setMyLocationStyle(myLocationStyle);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.basicmap:
                aMap.setMapType(AMap.MAP_TYPE_NORMAL);// 矢量地图模式
                break;
            case R.id.rsmap:
                aMap.setMapType(AMap.MAP_TYPE_SATELLITE);// 卫星地图模式
                break;
            case R.id.btn_show:
                showposition();
                break;
        }

    }

    public void showposition() {

        ArrayList<LatLng> values = new ArrayList<>();
        LatLng position1 = new LatLng(36.675807,117.000923);
        LatLng position = new LatLng(36.66789,116.942043);
        LatLng position2 = new LatLng(36.674164, 117.026269);
        LatLng position3 = new LatLng(39.755569, 116.790863);
        LatLng position4 = fromGpsToAmap(39.755569, 116.790863);
        values.add(position);
        values.add(position1);
        values.add(position2);
        values.add(position3);
        values.add(position4);
        Marker maker = aMap.addMarker(new MarkerOptions().position(position).icon(BitmapDescriptorFactory.fromResource(R.drawable.location)));
        Marker maker1 = aMap.addMarker(new MarkerOptions().position(position1).icon(BitmapDescriptorFactory.fromResource(R.drawable.location)));
        Marker maker2 = aMap.addMarker(new MarkerOptions().position(position2).icon(BitmapDescriptorFactory.fromResource(R.drawable.location)));
        Marker maker3 = aMap.addMarker(new MarkerOptions().position(position3).icon(BitmapDescriptorFactory.fromResource(R.drawable.location)));
        Marker maker4 = aMap.addMarker(new MarkerOptions().position(position4).icon(BitmapDescriptorFactory.fromResource(R.drawable.location)));
        aMap.moveCamera(CameraUpdateFactory.changeLatLng(position));
        drawLine(values);
    }
    public LatLng fromGpsToAmap(double lati,double longi) {
        LatLng position4 = null;
        CoordinateConverter converter = new CoordinateConverter(getActivity());
        converter.from(CoordinateConverter.CoordType.GPS);
        try {
            converter.coord(new DPoint(lati, longi));
            DPoint desLatLng = converter.convert();
            position4 = new LatLng(desLatLng.getLatitude(),desLatLng.getLongitude());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return position4;
    }
    /**绘制两个坐标点之间的线段,从以前位置到现在位置*/
    private void drawLine(ArrayList<LatLng> values) {
        aMap.addPolyline(new PolylineOptions().addAll(values).geodesic(true).color(Color.BLUE));
    }

    /**
     * 方法必须重写
     */
    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    public void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
}
